package com.hy.controller;

import cn.hutool.core.collection.ListUtil;
import com.hy.constans.HttpStatus;
import com.hy.domain.BlogComments;
import com.hy.domain.dto.CommentDTO;
import com.hy.domain.dto.R;
import com.hy.domain.dto.UserDTO;
import com.hy.exception.BusinessException;
import com.hy.service.BlogCommentsService;
import com.hy.util.UserHolder;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.stream.events.Comment;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("comments")
public class BlogCommentsController {
    @Autowired
    private BlogCommentsService blogCommentsService;

    /**
     * 添加评论
     * @param comments 评论实体类
     * @return
     */
    @PostMapping("/post")
    public R postComments(@RequestBody BlogComments comments) {
        final UserDTO user = UserHolder.getUser();
        if (user == null) {
            throw new BusinessException(HttpStatus.UNAUTHORIZED, "用户未登录");
        }
        System.out.println("comments = " + comments);
        comments.setCreateTime(LocalDateTime.now());
        comments.setLikeCount(0);
        comments.setUserId(user.getUserId());
        blogCommentsService.save(comments);
        return R.ok();
    }

    /**
     * 根据博客id查询评论
     * @param blogId
     * @return
     */
    @GetMapping("/{blogId}")
    public R listById(@PathVariable Long blogId) {
        List<CommentDTO> list = blogCommentsService.listCommentsByBlogId(blogId);

        return R.ok(list);
    }
}
